package com.giveu.signature.common.util;

import org.apache.commons.lang3.StringUtils;

import com.giveu.common.exception.ResultRuntimeException;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

public class FileUtil {
	
	public static void mkdir(String filePath,boolean readable,boolean writable,boolean executable){
		if(StringUtils.isBlank(filePath)){
			return ;
		}
		File file = new File(filePath);
		mkdir(file,readable,writable,executable);
	}
	
	public static void mkdir(File file,boolean readable,boolean writable,boolean executable){
		if(file.exists() && !file.isDirectory()){
			throw new IllegalArgumentException("文件路径不是一个文件夹");
		}
		
		if(!file.exists()){
			file.mkdirs();
			file.setReadable(readable, false);
			file.setWritable(writable,false);
			file.setExecutable(executable,false);
		}
	}
	
	public static void mk777dir(String filePath){
		mkdir(filePath,true,true,true);
	}
	
	
	public static void mk777dir(File file){
		mkdir(file,true,true,true);
	}
	
	
	public static void createFile(String filePath,boolean readable,boolean writable,boolean executable) throws IOException{
		if(StringUtils.isBlank(filePath)){
			return ;
		}
		File file = new File(filePath);
		createFile(file,readable,writable,executable);
	}
	
	public static void createFile(File file,boolean readable,boolean writable,boolean executable) throws IOException{
		File parentFile = file.getParentFile();
		
		if(parentFile != null && !parentFile.exists()){
			mk777dir(parentFile);
		}
		
		if(file != null && !file.exists()){
				file.createNewFile();
		}
		file.setReadable(readable,false);
		file.setWritable(writable,false);
		file.setExecutable(executable,false);
	}
	
	
	public static void create777File(String filePath) throws IOException{
		createFile(filePath, true,true, true);
	}
	
	public static void create777File(File file) throws IOException{
		createFile(file, true,true, true);
	}
	
	public static String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
	
	 public static String urlToFile(String photoUrl) {
	    	String path = System.getProperty("java.io.tmpdir")+UUID.randomUUID()+photoUrl.substring(photoUrl.lastIndexOf("."));
	    	URL url;
	    	DataInputStream in = null;
	    	DataOutputStream out = null;
			try {
				url = new URL(photoUrl);
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		    	connection.setRequestMethod("GET");
		    	in = new DataInputStream(connection.getInputStream());
		    	out = new DataOutputStream(new FileOutputStream(path));
		    	byte [] buffer = new byte[65536];
		    	int count = 0;
		    	while ((count = in.read(buffer)) > 0) {
		    	out.write(buffer, 0, count);
		    	}
		    	return path;
			} catch (Exception e) {
				throw new ResultRuntimeException("FAST文件获取异常",e);
			}finally {
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
}
